<?php

if (!class_exists("stwidgets")) {

    class stwidgets extends WP_Widget {

        public $view = null;
        public $config = array();
        public $actionName = "index";
        public $themePage = "index";

        public function __construct($id_base = false, $name, $widget_options = array(), $control_options = array()) {
            parent::__construct($id_base, $name, $widget_options, $control_options);
            $config = include TEMPLATEPATH . '/stInclude/config.php';
            $this->setConfig($config);

            $this->view = new stthemeview ();
            $this->view->config = $this->config;
            $this->option = get_option($this->config['theme_name'] . '_' . $this->config['option_key'] . '_settings');
        }

        protected function setConfig($config) {
            $this->config = $config;
            if ($this->actionName == "index") {
                $this->actionName = $config['default_action'];
            }

            $this->config['themeDir'] = TEMPLATEPATH . '/admin-functions/';
            $this->config['includePath'] = TEMPLATEPATH . '/stInclude/';
            $this->config['cssUrl'] = WP_CONTENT_URL . '/themes/' . $this->config['theme_name'] . '/admin-functions/css/';
            $this->config['scriptUrl'] = WP_CONTENT_URL . '/themes/' . $this->config['theme_name'] . '/admin-functions/scripts/';
            $this->config['imagesUrl'] = WP_CONTENT_URL . '/themes/' . $this->config['theme_name'] . '/admin-functions/images/';
        }

        function widget($args, $instance) {
            $aciontName = $instance['id_base'] . 'ActionFrontend';
            $method = array($this, $aciontName);
            if (is_callable($method))
                $this->$aciontName();
            $data = $this->view->getData();
            $widget_view = $this->config['themeDir'] . "views/widgets/{$this->id_base}_frontend.php";
            echo get_include_contents($widget_view, array_merge($instance, $data));
        }

        function update($new_instance, $old_instance) {

            $search = array("\\'", '\\"');
            $replace = array("'", '"');
            $data = str_replace($search, $replace, $_POST);
            return $data;
        }

        function form($instance) {
            $aciontName = $instance['id_base'] . 'ActionFrontend';
            $method = array($this, $aciontName);
            if (is_callable($method))
                $this->$aciontName();
            $data = $this->view->getData();
            $widget_view = $this->config['themeDir'] . "views/widgets/{$this->id_base}_backend.php";
            echo get_include_contents($widget_view, array_merge($instance, $data));
        }

        public function loadmodel($modelName) {
            $className = $modelName . "_model";
            $fileName = $this->config['includePath'] . "models/{$modelName}.php";
            require_once $fileName;
            $obj = new $className($this->config);
            return $obj;
        }

        protected function updateOption($data = '', $value = '') {
            $option = get_option($this->config['theme_name'] . '_' . $this->config['option_key'] . '_settings');
            if (is_array($data) > 0) {
                foreach ($data as $key => $value) {
                    $option[$key] = $value;
                }
            } else {
                $option[$data] = $value;
            }

            if (update_option($this->config['theme_name'] . '_' . $this->config['option_key'] . '_settings', $option)) {
                return true;
            } else {
                return false;
            }
        }

    }

}
?>